package com.shop.shoporder.service.impl;

import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.InterfaceVer;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.entity.order.Cart;
import com.shop.shoporder.entity.order.GoodsComment;
import com.shop.shoporder.mapper.order.CartMapper;
import com.shop.shoporder.mapper.order.GoodsCommentMapper;
import com.shop.shoporder.service.GoodsCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("goodsCommentService")
public class GoodsCommentServiceImpl implements GoodsCommentService {
    @Autowired
    GoodsCommentMapper goodsCommentMapper;
    @Autowired
    CartMapper cartMapper;
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return goodsCommentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(GoodsComment record) {
        return goodsCommentMapper.insert(record);
    }

    @Override
    public int insertSelective(GoodsComment record) {
        return goodsCommentMapper.insertSelective(record);
    }

    @Override
    public GoodsComment selectByPrimaryKey(Integer id) {
        return goodsCommentMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(GoodsComment record) {
        return goodsCommentMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(GoodsComment record) {
        return goodsCommentMapper.updateByPrimaryKey(record);
    }

    @Transactional
    @Override
    public Response goodsComment(Map<String, String> map, HttpServletRequest request) throws Exception {
        int userNoId=0;
        int productId=0;
        int guigeId=0;
        String comment="";
        String orderNo="";
        GoodsComment goodsComment=new GoodsComment();
        if(map!=null) {
            userNoId = InterfaceVer.VerNumber(map.get("userNoId"));
            productId = InterfaceVer.VerNumber(map.get("productId"));
            guigeId= InterfaceVer.VerNumber(map.get("guigeId"));
            comment= InterfaceVer.VerStr(map.get("comment"));
            orderNo= InterfaceVer.VerStr(map.get("orderNo"));
            goodsComment.setUsernoId(userNoId);
            goodsComment.setProductId(productId);
            goodsComment.setGuigeId(guigeId);
            goodsComment.setComment(comment);
            goodsComment.setOrderNo(orderNo);
            goodsComment.setCommentTime(new Date());
            if(productId==0||guigeId==0){
                return new Response(MessageConstants.MSG_ADD_FAILED,null,0,"评论失败!",request);
            }
            int res=goodsCommentMapper.insertSelective(goodsComment);
            if(res>0){
                return new Response(MessageConstants.MSG_SUCCESS,null,0,"评论成功!",request);
            }else {
                return new Response(MessageConstants.MSG_ADD_FAILED,null,0,"评论失败!",request);
            }
        }else {
            return new Response(MessageConstants.MSG_ADD_FAILED,null,0,"评论失败!",request);
        }
    }

    @Override
    public Response commentList(Map<String, String> map, HttpServletRequest request) throws Exception {
        int userNoId=0;
        int type=0;
        int productId=0;
        List<Cart> cartList=new ArrayList<>();
        if(map!=null) {
            userNoId = InterfaceVer.VerNumber(map.get("userNoId"));
            type= InterfaceVer.VerNumber(map.get("type"));
            productId= InterfaceVer.VerNumber(map.get("productId"));
            cartList=cartMapper.getOrderCommentGoodsList(userNoId,type,productId);
            return new Response(MessageConstants.MSG_SUCCESS,cartList,0,"获取成功!",request);
        }
        return new Response(MessageConstants.MSG_SUCCESS,cartList,0,"获取成功!",request);
    }
}
